package com.whosly.mysql.protocol.packet.impl.ok.data;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * @author 风杨
 * @see <a href="yueny09@163.com">mailTo: yueny09@163.com</a>
 */
class ElapsedItemFormat implements Serializable {
    // 获取正常的数字格式
    private static final NumberFormat numberFormat = DecimalFormat.getNumberInstance();
    private static final DecimalFormat decimalFormat = (DecimalFormat) numberFormat;
    static {
        // 保留小数点后面1位， 不足的补0. 前面整数部分，每隔3位， 用 ， 分开
        decimalFormat.applyPattern("#,###.0");
    }

    // elapsedMillCost Unit
    private static final String FORMAT = "Elapsed cost %s %s,. ";

    /**
     * @param elapsedMillCost 执行总耗时
     */
    public static final String format(Long elapsedMillCost){
        if(elapsedMillCost == null || elapsedMillCost < 0){
            return "";
        }

        try{
            return String.format(FORMAT, decimalFormat.format(elapsedMillCost), "ms");
        }catch (Exception ex){
            //.
        }
        return "";
    }

    /**
     * @param elapsedMillCost 执行耗时
     * @param queryElapsedMillCost 查询阶段耗时(包含执行计划耗时)
     * @param queryExplanElapsedMillCost 执行计划耗时
     */
    public static final String format(Long elapsedMillCost,
                                      Long queryElapsedMillCost, Long queryExplanElapsedMillCost){
        if(elapsedMillCost == null){
            return "";
        }

        try {
            return String.format(FORMAT, decimalFormat.format(elapsedMillCost), "ms");
        }catch (Exception ex){
            //.
        }
        return "";
    }

}
